<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<%@ taglib prefix="s" uri="/struts-tags"%>

<s:include value="/comm/header.jsp" />
<s:include value="/comm/menu.jsp"/>
<!--第1部分开始  --> 
    <div class="wrap1">
    	<div class="blank1"></div>
    	<s:form name="query" action="wantBuyList" method="get" id="search"> 
    	<s:hidden id="categoryId" name="category" />
    	<s:hidden id="areaId" name="area" />
    	<s:hidden id="pubtime" name="pubtime" />
    	<s:hidden id="pageSize" name="pageSize" />
    	<s:hidden id="order" name="o" />
    	<input type="hidden" id="superPage" name="superPage" value="1" />
        <div class="outbox2">
        	<div class="sort_list1">
            	<ul>
            		<li>
						【类别】 
						<s:iterator value="categorys" status="st">
            				&nbsp;&nbsp;
            				<s:if test="categoryId==category">
								<span class="span21"><s:property value="categoryName" /></span>
							</s:if>
							<s:else>
								<a
									href="javascript:query(1, <s:property value='categoryId' />);"><s:property
											value="categoryName" /></a>
							</s:else>
						</s:iterator>
					</li>
					<li>
					   【区域】 <s:iterator value="areas">
            			&nbsp;&nbsp;
            			<s:if test="areaId == area">
								<span class="span21"><s:property value="areaName" /></span>
							</s:if>
							<s:else>
								<a
									href="javascript:query(2, <s:property value='areaId' />);"><s:property
											value="areaName" /></a>
							</s:else>
						</s:iterator>
					</li>
					<li>
						【发布时间】 
						<s:iterator value='#{"0":"不限", "1":"1周内","2":"1月内","3":"三个月内" }'>
                			&nbsp;&nbsp;
                			<s:if test="pubtime == key">
								<span class="span21"><s:property value="value" /></span>
							</s:if>
							<s:else>
								<a href="javascript:query(3, <s:property value='key' />);"><s:property
											value="value" /></a>
							</s:else>
						</s:iterator>
					</li>
                </ul>
            </div>
        <div class="bg_line2">
        	<div class="span22">输入关键字：</div>
            <div class="searchbox1">
            	<s:textfield name="q" cssClass="input2"  />
            	<input name="" type="button" value="搜索" class="input3" id="btnSearch"/>
            </div>
            <div class="span23"><input name="" type="button" value="重置所有条件" class="input4" onclick="javascript:window.location.href='wantBuyList.htm'"/></div>
            <div class="clear"></div>
        </div>
        </div>
        </s:form>
    	
            
        
        <div class="blank1"></div>
        <div class="wrap_left">
            <div class="w_box4" style="width:1000px">

<!--开始 -->
<div class="outbox2">   
			<div class="agent_show_tt">
            	<span class="span24">共有 <span class="showtxt4"><s:property value="totalSize" /></span> 条求购信息 </span>
            	<span class="span25"><strong>每页显示：</strong>
					<s:if test="pageSize == 10">
            		10&nbsp;&nbsp;<a href="javascript:setPageSize(20);">20</a>&nbsp;&nbsp;<a href="javascript:setPageSize(30);">30</a>	
            	</s:if>
            	<s:elseif test="pageSize == 20">
            		<a href="javascript:setPageSize(10);">10</a>&nbsp;&nbsp;20&nbsp;&nbsp;<a href="javascript:setPageSize(30);">30</a>
            	</s:elseif>
            	<s:elseif test="pageSize == 30">
            		<a href="javascript:setPageSize(10);">10</a>&nbsp;&nbsp;<a href="javascript:setPageSize(20);">20</a>&nbsp;&nbsp;30
            	</s:elseif>
            	<s:else>
            		<a href="javascript:setPageSize(10);">10</a>&nbsp;&nbsp;<a href="javascript:setPageSize(20);">20</a>&nbsp;&nbsp;<a href="javascript:setPageSize(30);">30</a>
            	</s:else>
				</span>
            </div>
            <div class="clear"></div>   
        <!--列表开始-->
<div class="show_list2">
	<div class="show_list2_box1">
    	<!--循环开始-->
    	<div class="show_list3_con" id="wantBuys">
    		<s:if test="totalSize == 0">
    			抱歉！没有找到相关的求购信息。
    		</s:if>
    		<s:else>
    			<ul>
            		<li class="t1 tt"><span class="box">&nbsp;</span></li>
                	<li class="t2 tt"><span class="box">名称</span></li>
                	<li class="t3 tt tt0"><span class="box"><a href="javascript:doSort(1);">所属区域</a></span></li>
                	<li class="t4 tt tt0"><span class="box"><a href="javascript:doSort(2);">截止日期</a></span></li>
                	<li class="t5 tt tt0"><span class="box"><a href="javascript:doSort(3);">代购服务费</a></span></li>
                	<li class="t6 tt"><span class="box">&nbsp;</span></li>
				</ul>
				<s:iterator value="wantBuyModels" status="st">
					  <ul class="ttbox">
					  	<li class="t1"><div class="pbox1_list_pic"><div><p><a href="wantBuyDetail.htm?id=<s:property value='wantId'/>" target="_blank"><img src="<s:property value='image50' />"/></a></p></div></div></li>
                		<li class="t2"><span class="box"><a href="wantBuyDetail.htm?id=<s:property value='wantId'/>" target="_blank"><s:property value="goodsName" /></a></span></li>
                		<li class="t3"><span class="box"><s:property value="areaName" /></span></li>
               			<li class="t4"><span class="box"><s:property value="validityPeriod" /></span></li>
               			<li class="t5">
               				<span class="box">
               					<span class="span19">
									￥<s:property value="serviceFee" />
               					</span>
               				</span>
               			</li>
                		<li class="t6">
                			<span class="box">
								<a href="javascript:invite(${wantId}, ${user.userid });" class="but01">我来代购</a>
								<a href="javascript:rideBuy(${wantId}, ${user.userid });" class="but02">搭车求购</a>
                			</span>
                		</li>
					</ul>
				</s:iterator>
    		</s:else>
         	<div class="clear"></div>
        </div>
        <!--循环结束-->

        <div class="clear"></div>

      
    </div>
    
    
</div>  
        
        
<!--列表结束-->
</div> 
  <!--页码开始-->
		<div class="page_box">
			<div class="pagewrap">
				<div class="page" id="page">
					<s:if test="totalSize > 0">
						<ul>
							<s:if test="superPage == 1">
								<li>首页</li>
								<li>上一页</li>
							</s:if>
							<s:else>
								<li><a href="javascript:page(1)">首页</a></li>
								<li><a href="javascript:page(<s:property value='superPage' /> - 1)">上一页</a></li>
							</s:else>
							<s:iterator value="pageList">
								<s:if test="pageNumber == '...'">
									<li><s:property value='pageNumber' /></li>
								</s:if>
								<s:elseif test="choose">
									<li class="thisclass"><s:property value='pageNumber' /></li>
								</s:elseif>
								<s:else>
									<li><a href="javascript:page(<s:property value='pageNumber' />)"><s:property value='pageNumber' /></a></li>
								</s:else>
								
							</s:iterator>
							<s:if test="superPage == totalPage">
								<li>下一页</li>
								<li>末页</li>
							</s:if>
							<s:else>
								<li><a href="javascript:page(<s:property value='superPage' /> + 1)">下一页</a></li>
								<li><a href="javascript:page(<s:property value='totalPage' />)">末页</a></li>
							</s:else>
							<li><span class="pageinfo">共 <strong><s:property value='totalPage' /></strong>页<strong><s:property value='totalSize' /></strong>条
							</span></li>
						</ul>
					</s:if>

				</div>
			</div>
		</div>
		<!--页码结束 -->
            
            </div>


        </div>


     

    </div>
    
    
<s:include value="/comm/bottom.jsp"/>


<script type="text/javascript">
	$(window).load(function() {
		setTab('tabe',2,3);
	});

	$("#btnSearch").click(function() {
		$("#search").submit();
	});
	
	var _wantId;
	var _userId;
	function invite(wantId, userId) {
		_wantId = wantId;
		_userId = userId;
		var userObj = "${sessionScope.user}";
		if(userObj == '' && !bLogin){
			to = 6;
			showLogin();
		}else {
			if (_userId == lUserId) {
				queryForAjax();
			}
			else {
				$.ajax( {
					url:'canInvite.htm',
					data:{"inviteModel.wantId":_wantId},
					async:false,
					success:function(json) {
						if (json.ajaxMsg != null) {
							alert(json.ajaxMsg);
						}	
						else {
							window.location.href = "invite.htm?id=" + _wantId;
						}
					}
				});
			}
		}
	}
	
	function rideBuy(wantId, userId) {
		_wantId = wantId;
		_userId = userId;
		
		var userObj = "${sessionScope.user}";
		if(userObj == '' && !bLogin){
			to = 7;
			showLogin();
		}else {
			if (_userId == lUserId) {
				queryForAjax();
			}
			else {
				$.ajax( {
					url:'canRideBuy.htm',
					data:{"simpleBuyModel.wantBuyModel.wantId":_wantId},
					success:function(json){
						if (json.ajaxMsg != null) {
							alert(json.ajaxMsg);
						}
						else {
							window.location.href= "rideBuy.htm?id=" + _wantId;
						}
					}
				});
			}
		}
	}
	
	function queryForAjax() {
		var categoryId = $("#categoryId").val();
		var areaId = $("#areaId").val();
		var pubtime = $("#pubtime").val();
		var superPage = $("#superPage").val();
		var pageSize = $("#pageSize").val();
		$.ajax({
			url:"wantBuyList.htm",
			data:{"wantBuyModel.categoryId":categoryId,"wantBuyModel.areaId":areaId,"pubtime":pubtime,"superPage":superPage,"pageSize":pageSize,"requestType":1},
			success:showWantBuys
		});
	}
	
	function showWantBuys(json) {
		if (json.ajaxMsg != null) {
			$("#wantBuys").html("<span style='color:red'>" + json.ajaxMsg + "</span>");
			$("#page").html("");
		}
		else {
			var wantBuysHtml = "";
			var wantBuyModels = json.wantBuyModels;
			if (wantBuyModels.length == 0) {
				wantBuysHtml = "抱歉！没有找到相关的求购信息。";
			}
			else {
				wantBuysHtml += "<ul>";
				wantBuysHtml += "<li class='t1 tt'><span class='box'>&nbsp;</span></li>";
				wantBuysHtml += "<li class='t2 tt'><span class='box'>名称</span></li>";
				wantBuysHtml += "<li class='t3 tt tt0'><span class='box'><a href='javascript:doSort(1);'>所属区域</a></span></li>";
				wantBuysHtml += "<li class='t4 tt tt0'><span class='box'><a href='javascript:doSort(2);'>截止日期</a></span></li>";
				wantBuysHtml += "<li class='t5 tt tt0'><span class='box'><a href='javascript:doSort(3);'>代购服务费</a></span></li>";
				wantBuysHtml += "<li class='t6 tt'><span class='box'>&nbsp;</span></li>";
				wantBuysHtml += "<div class='clear'></div>";
				wantBuysHtml += "</ul>";
				for (var i = 0; i < wantBuyModels.length; i++) {
					wantBuysHtml += "<ul class='ttbox'>";
					wantBuysHtml += "<li class='t1'><div class='pbox1_list_pic'><div><p><img src='" + wantBuyModels[i].image50 + "'/></p></div></div></li>";
					wantBuysHtml += "<li class='t2'><span class='box'><a href='wantBuyDetail.htm?wantBuyModel.wantId=" + wantBuyModels[i] + "' target='_blank'>" + wantBuyModels[i].goodsName + "</a></span></li>";
					wantBuysHtml += "<li class='t3'><span class='box'><a href='javascript:doQuery(1, " + wantBuyModels[i].areaId + ");'>" + wantBuyModels[i].areaName + "</a></span></li>";
					wantBuysHtml += "<li class='t4'><span class='box'>" + wantBuyModels[i].validityPeriod  + "</span></li>";
					wantBuysHtml += "<li class='t5'>";
					wantBuysHtml += "<span class='box'>";
					wantBuysHtml += "<span class='span19'>";
					wantBuysHtml += "￥" + wantBuyModels[i].serviceFee;
					wantBuysHtml += "</span></span></li>";
					wantBuysHtml += "<li class='t6'>";
					wantBuysHtml += "<span class='box'>";
					wantBuysHtml += "<a href='javascript:invite(" + wantBuyModels[i].wantId + "," + wantBuyModels[i].user.userid + ");' class='but01'><p>我来代购</p></a>";
					wantBuysHtml += "<a href='javascript:rideBuy(" + wantBuyModels[i].wantId + "," +  wantBuyModels[i].user.userid + ");' class='but02'><p>搭车求购</p></a>";
					wantBuysHtml += "</span></li>";
					wantBuysHtml += "<div class='clear'></div>";
					wantBuysHtml += "</ul>";
				}
				
				var pageHtml = "";
				if (json.totalSize > 0) {
					pageHtml += "<ul>";
					if (json.superPage == 1) {
						pageHtml += "<li>首页</li>";
						pageHtml += "<li>上一页</li>";
					}
					else {
						pageHtml += "<li><a href='javascript:page(1)'>首页</a></li>";
						pageHtml += "<li><a href='javascript:page(" + (json.superPage - 1) + ")'>上一页</a></li>";
					}
					var pageList = json.pageList;
					for (var i = 0; i < pageList.length; i++) {
						if (pageList[i].pageNumber == "...") {
							pageHtml += "<li>" + pageList[i].pageNumber + "</li>";
						}
						else if (pageList[i].choose) {
							pageHtml += "<li class='thisclass'>" + pageList[i].pageNumber + "</li>";
						}
						else {
							pageHtml += "<li><a href='javascript:page(" + pageList[i].pageNumber + ")'>" + pageList[i].pageNumber + "</a></li>";
						}
						pageHtml += "<li><span class='pageinfo'>共 <strong>" + json.totalPage  + "</strong>页<strong>" + json.totalSize + "</strong>条<span></li>";
						pageHtml += "<div class='clear'></div>";
						pageHtml += "</ul>";
					}
				}
				$("#wantBuys").html(wantBuysHtml);
				$("#page").html(pageHtml);
				
			}
		}
	}
	
	function setPageSize(pageSize) {
		$("#pageSize").val(pageSize);
		$("#search").submit();
	}
	
	function page(superPage) {
		$("#superPage").val(superPage);
		$("#search").submit();
	}
	

	
	function query(type, value) {
		if (type == 1) {
			$("#categoryId").val(value);
		}else if (type == 2) {
			$("#areaId").val(value);
		}else if(type == 3) {
			$("#pubtime").val(value);
		}
		$("#search").submit();
	}

	function doSort(orderField) {
		var sortValue = $("#order").val();
		//有效期排序
		if (orderField == 1) {
			if (sortValue == 0) {
				sortValue = 1;
			}
			else {
				sortValue = 0;
			}
		}
		else if (orderField == 2) {
			if (sortValue == 2) {
				sortValue = 3;
			}
			else {
				sortValue = 2;
			}
		}
		else if (orderField == 3) {
			if (sortValue == 4) {
				sortValue = 5;
			}	
			else {
				sortValue = 4;
			}
		}
		$("#order").val(sortValue);
		$("#search").submit();
	}
</script>    